<template>
  <div class="shop-list">
    <div class="page-header">
      <h2>商品管理</h2>
      <el-button type="primary" @click="handleAddProduct">添加商品</el-button>
    </div>
    
    <el-card class="box-card">
      <div class="search-bar">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="商品名称">
            <el-input placeholder="请输入商品名称" clearable></el-input>
          </el-form-item>
          <el-form-item label="商品类型">
            <el-select placeholder="请选择" clearable>
              <el-option label="运动器材" value="1"></el-option>
              <el-option label="健康食品" value="2"></el-option>
              <el-option label="保健品" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary">查询</el-button>
            <el-button>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <el-table
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="80">
        </el-table-column>
        <el-table-column
          prop="image"
          label="商品图片"
          width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="50" :src="scope.row.image || 'https://placeholder.pics/svg/50'">
            </el-avatar>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="商品名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="price"
          label="价格"
          width="100">
        </el-table-column>
        <el-table-column
          prop="stock"
          label="库存"
          width="100">
        </el-table-column>
        <el-table-column
          prop="type"
          label="类型"
          width="100">
        </el-table-column>
        <el-table-column
          prop="createdAt"
          label="上架时间"
          width="180">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          width="100">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status === 1">上架中</el-tag>
            <el-tag type="info" v-else>已下架</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180">
          <template #default="scope">
            <el-button type="text" size="small" @click="handleView(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="handleToggleStatus(scope.row)">
              {{ scope.row.status === 1 ? '下架' : '上架' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { useRouter } from 'vue-router'

export default {
  setup() {
    const router = useRouter()
    
    return {
      router
    }
  },
  data() {
    return {
      tableData: [
        {
          id: 1,
          name: '智能手环',
          image: 'https://placeholder.pics/svg/50/3498DB/FFFFFF/智能手环',
          price: '299.00',
          stock: 100,
          type: '运动器材',
          createdAt: '2023-10-15 14:30:00',
          status: 1
        },
        {
          id: 2,
          name: '蛋白粉',
          image: 'https://placeholder.pics/svg/50/27AE60/FFFFFF/蛋白粉',
          price: '128.00',
          stock: 50,
          type: '健康食品',
          createdAt: '2023-10-20 09:15:00',
          status: 1
        },
        {
          id: 3,
          name: '运动套装',
          image: 'https://placeholder.pics/svg/50/9B59B6/FFFFFF/运动套装',
          price: '458.00',
          stock: 30,
          type: '运动器材',
          createdAt: '2023-10-25 11:45:00',
          status: 0
        }
      ],
      total: 3
    }
  },
  methods: {
    handleAddProduct() {
      // 跳转到添加商品页面
      this.router.push('/admin/shop/add');
    },
    handleView(row) {
      // 跳转到商品详情页面
      this.router.push(`/admin/shop/detail/${row.id}`);
    },
    handleEdit(row) {
      // 跳转到商品编辑页面
      this.router.push(`/admin/shop/edit/${row.id}`);
    },
    handleToggleStatus(row) {
      // 弹出确认对话框
      const action = row.status === 1 ? '下架' : '上架';
      this.$confirm(`确认${action}该商品吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 这里添加上架/下架商品的逻辑
        row.status = row.status === 1 ? 0 : 1;
        this.$message({
          type: 'success',
          message: `${action}成功!`
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    }
  }
}
</script>

<style scoped>
.shop-list {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-bar {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 